package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private TbClueTrackRecordMapper trackRecordMapper;


    /**
     * 查询线索跟进记录列表
     *
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        List<TbClueTrackRecord> tbClueTrackRecord = trackRecordMapper.list(clueId);
        return tbClueTrackRecord;
    }

    /**
     * 新增线索跟进记录
     *
     * @param
     */
    @Override
    @Transactional
    public Integer add(ClueTrackRecordVo clueTrackRecord) {
        //获取当前的跟进人
        String username = SecurityUtils.getUsername();
        //获取当前跟进时间
        Date now = DateUtils.getNowDate();
        //封装到线索跟进记录对象
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecord,tbClueTrackRecord);
        //把跟进人和跟进时间封装到对象中
        tbClueTrackRecord.setCreateTime(now);
        tbClueTrackRecord.setCreateBy(username);
        //创建线索管理对象
        TbClue tbClue = new TbClue();
        //把对应属性封到线索管理对象中
        BeanUtils.copyProperties(clueTrackRecord,tbClue);
        tbClue.setId(clueTrackRecord.getClueId());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        //修改线索数据
        tbClueMapper.updateTbClue(tbClue);
        //添加线索跟进记录
        return trackRecordMapper.add(tbClueTrackRecord);
    }


}
